﻿using UnityEngine;
using UnityEngine.Events;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif


public class Mouse3D : MonoBehaviour
{

    public static Vector2 MousePosition
    {
        get
        {
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
            return Mouse.current.position.ReadValue();
#else
            return Input.mousePosition;
#endif
        }
    }
    public static bool LeftButtonDown
    {
        get
        {
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
            return Mouse.current.leftButton.wasPressedThisFrame;
#else
            return Input.GetMouseButtonDown(0);
#endif
        }
    }
    [SerializeField] private LayerMask mouseColliderLayerMask = new LayerMask();

    private void Start()
    {
        Cursor.visible = true;
        Cursor.lockState = CursorLockMode.None;
    }
    private static Mouse3D instance;

    public static Mouse3D Instance
    {
        get
        {
            if (instance == null)
            {
                // 在场景中查找是否已存在该类型的实例
                instance = FindObjectOfType<Mouse3D>();
                // 如果场景中不存在该类型的实例，则创建一个新的GameObject并添加该组件
                if (instance == null)
                {
                    GameObject singletonObject = new GameObject(typeof(Mouse3D).Name + "_Singleton");
                    instance = singletonObject.AddComponent<Mouse3D>();
                    DontDestroyOnLoad(singletonObject); // 保留在场景切换时不被销毁
                }
            }
            return instance;
        }
    }

    private Camera _camera;
//主相机下 鼠标下物体
    public UnityAction<GameObject> OnMouseDownObj = delegate(GameObject arg0) {  };
    private void Update()
    {
        if (!_camera) _camera = Camera.main;
        Ray ray = _camera.ScreenPointToRay(MousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask))
        {
            transform.position = raycastHit.point;
            if (LeftButtonDown)
            {
                OnMouseDownObj.Invoke(raycastHit.collider.gameObject);
            }
        }
    }

    public static Vector3 GetMouseWorldPosition() => Instance.GetMouseWorldPosition_Instance();
    public static bool GetMouseWorldHit(LayerMask layerMask, out RaycastHit hit)
    {
        Ray ray = Camera.main.ScreenPointToRay(MousePosition);

        return Physics.Raycast(ray, out hit, 999f, layerMask);

    }
    private Vector3 GetMouseWorldPosition_Instance()
    {   
        Ray ray = Camera.main.ScreenPointToRay(MousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask)) 
        {
            //防止重叠面
            raycastHit.point += raycastHit.normal * 0.001f;
            return raycastHit.point;
        }
        else
        {
            return Vector3.zero;
        }
    }
    private void OnDrawGizmosSelected()
    {

        Ray ray = Camera.main.ScreenPointToRay(MousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, mouseColliderLayerMask))
        {
            Gizmos.DrawSphere(    raycastHit.point, 0.1f);
        }
        Gizmos.DrawRay(Camera.main.transform.position, ray.direction * 50);
    }
}
